package com.tools.sqlmaker;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.tools.sqlmaker.entry.OrgInfo;
import com.tools.sqlmaker.factory.NormalFactory;
import com.tools.sqlmaker.factory.RabbitMqFactory;
import com.tools.sqlmaker.util.BaseFileUtil;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.tools.sqlmaker.constant.SysConstant.RABBITMQ;

/**
 * todo: 队列名修改
 */
public class Main {

    public static void main(String[] args) {

//        String filePath = "C:\\Users\\20231\\Desktop\\新的文档 1.sql";
//        String orgPath = "D:\\IdeaProject\\sql-maker\\src\\main\\resources\\json接入机构数据.json";

        String dataBase = args[0];
        String sourceFileName = args[1];
        String tarFileName = args[2];

        String projectPath = null;

        // 初始化路径
        // 可以通过 jvm 变量 workdir 指定运行路径
        // -Dworkdir=/xxx
        String workdir = System.getProperty("workdir");
        if (null != workdir) {
            projectPath = workdir.replace("\\", "/");
        } else {
            // 默认以命令执行任务为当前项目路径
            projectPath = System.getProperty("user.dir");
        }

        String paramStr = FileUtil.readString(new File(projectPath, sourceFileName), Charset.defaultCharset());

        List<OrgInfo> orgInfoList = JSONUtil.toList(paramStr, OrgInfo.class);

        List<String> allList = new ArrayList<>();
        for (OrgInfo orgInfo : orgInfoList) {
            orgInfo.setDataBase(dataBase);

            if (Objects.equals(orgInfo.getQType(), RABBITMQ)) {
                orgInfo.setMgrNum(1);
                allList.addAll(new RabbitMqFactory().make(orgInfo));
            } else {
                allList.addAll(new NormalFactory().make(orgInfo));
            }
        }

        BaseFileUtil.writeFile(new File(projectPath, tarFileName), allList);
    }
}